package com.xy.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.TextBook;
import com.xy.service.TextBookService;
import com.xy.service.impl.TextBookServiceImpl;

/**
 * Servlet implementation class textBookServlet
 */
@WebServlet("/textBookServlet")
public class TextBookServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    this.doPost(request, response);
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub

    TextBookService tbService = new TextBookServiceImpl();

    if ("add".equals(request.getParameter("status"))) {
      TextBook textBook = new TextBook();
      textBook.setIsbn(request.getParameter("isbn"));
      textBook.setName(request.getParameter("name"));
      textBook.setAuthor(request.getParameter("author"));
      textBook.setPublishCompany(request.getParameter("publishCompany"));
      textBook.setPunlishTime(request.getParameter("punlishTime"));
      textBook.setPrice(Double.valueOf(request.getParameter("price")));
      textBook.setRemark(request.getParameter("remark"));
      textBook.setEdit(request.getParameter("edit"));
      textBook.setCreateDate(new Date());

      if (tbService.add(textBook)) {
        request.getRequestDispatcher("/textBookServlet?status=list&currentPage=0").forward(request,
            response);
      } else {
        System.out.println("error");
      }

    } else if ("list".equals(request.getParameter("status"))) {
      int currentPage = 0;
      if (request.getParameter("goalpage") == null) {
        currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
      } else
        currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

      PageResults pageResults = tbService.listByPage(currentPage);

      request.setAttribute("pageResults", pageResults);
      request.setAttribute("status", "list");

      if("add".equals(request.getParameter("pre"))) {
    	  if(request.getParameter("page")!=null) {
    		  request.getSession().setAttribute("page", request.getParameter("page"));
    	  }
    	  request.getRequestDispatcher("/pages/storage/storage001/student/query_book.jsp").forward(request,
					response);
      }else
    	  request.getRequestDispatcher("/pages/book/book001/book_list.jsp").forward(request, response);

    } else if ("query".equals(request.getParameter("status"))) {

      int currentPage = 0;
      if (request.getParameter("goalpage") == null) {
        currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
      } else
        currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

      String name = null;
      String author = null;
      String publishCompany = null;
      String list = null;//查询条件合成String

      if (request.getParameter("list") == null) {
        name = request.getParameter("name") == null ? "%" : request.getParameter("name");
        author = request.getParameter("author") == null ? "%" : request.getParameter("author");
        publishCompany = request.getParameter("publishCompany") == null ? "%"
            : request.getParameter("publishCompany");
        list = name + "," + author + "," + publishCompany;
      } else {
        String str = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
        String[] strs = str.split(",");
        for (int i = 0; i < strs.length; i++) {
          if (i == 0)
            name = i == 0 ? strs[i] : "%";
          if (i == 1)
            author = i == 1 ? strs[1] : "%";
          if (i == 2)
            publishCompany = i == 2 ? strs[2] : "%";
        }
        list = name + "," + author + "," + publishCompany;
      }


      PageResults pageResults = tbService.query(name, author, publishCompany, currentPage);

      request.setAttribute("pageResults", pageResults);
      request.setAttribute("status", "query");
      request.setAttribute("list", list);

      if("add".equals(request.getParameter("pre"))) {
    	  if(request.getParameter("page")!=null) {
    		  request.getSession().setAttribute("page", request.getParameter("page"));
    	  }
    	  request.getRequestDispatcher("/pages/storage/storage001/student/query_book.jsp").forward(request,
					response);
      }else
    	 request.getRequestDispatcher("/pages/book/book001/book_list.jsp").forward(request, response);
    } else if ("del".equals(request.getParameter("status"))) {

      int id = Integer.valueOf(request.getParameter("id"));

      TextBook tb = new TextBook();
      tb.setId(id);

      if (tbService.delete(tb)) {
        request.getRequestDispatcher("/textBookServlet?status=list&currentPage=0").forward(request,
            response);
      } else {
        System.out.println("error");
        request.getRequestDispatcher("/textBookServlet?status=list&currentPage=0").forward(request,
            response);
      }

    } else if ("up".equals(request.getParameter("status"))) {
      int id = Integer.valueOf(request.getParameter("id"));
      TextBook textBook = tbService.get(id);
      textBook.setIsbn(request.getParameter("isbn"));
      textBook.setName(request.getParameter("name"));
      textBook.setAuthor(request.getParameter("author"));
      textBook.setPublishCompany(request.getParameter("publishCompany"));
      textBook.setPunlishTime(request.getParameter("punlishTime"));
      textBook.setPrice(Double.valueOf(request.getParameter("price")));
      textBook.setRemark(request.getParameter("remark"));
      textBook.setEdit(request.getParameter("edit"));
      
      if(tbService.update(textBook)) {
        request.getRequestDispatcher("/textBookServlet?status=list&currentPage=0").forward(request,
            response);
      }else {
        System.out.println("error");
        request.getRequestDispatcher("/textBookServlet?status=list&currentPage=0").forward(request,
            response);
      }
      
      
    } else if ("detail".equals(request.getParameter("status"))) {
      int id = Integer.valueOf(request.getParameter("id"));
      TextBook tb = tbService.get(id);
      request.setAttribute("tb", tb);
      if("up".equals(request.getParameter("pre")))
        request.getRequestDispatcher("/pages/book/book001/book_modify.jsp?").forward(request,
            response);
      else
        request.getRequestDispatcher("/pages/book/book001/book_detail.jsp").forward(request,
            response);
    }



  }

}
